#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 青城子
# datetime： 2021/9/2 21:26 
# ide： PyCharm

import requests
import execjs
from lxml import etree


class WanMei:

    def __init__(self, password="123456", exe_function="getPwd"):
        self.password = password
        self.exe_function = exe_function
        self.url = "https://passport.wanmei.com/sso/login?service=passport&isiframe=1&location=2f736166652f"
        self.headers = {
            "User - Agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 84.0.4147105Safari / 537.36"
        }
        self.node = execjs.get()

    def get_pub_key(self):
        """
        获取input中的key
        :return:
        """
        page_text = requests.get(url=self.url, headers=self.headers).text
        tree = etree.HTML(page_text)
        return tree.xpath('//input[@id="e"]/@value')[0]

    def reverse_engineer(self):
        key = self.get_pub_key()
        ctx = self.node.compile(open("../static/wanmei.js", encoding="utf-8").read())
        # 3.执行js函数
        func_name = "{0}('{1}','{2}')".format(self.exe_function, self.password,
                                              key)  # getPwd()是在wangmei.js中定义的加密函数，传入需要加密的参数
        print(func_name)
        pwd = ctx.eval(func_name)  # 执行获取加密后的pwd
        return pwd


if __name__ == '__main__':
    obj = WanMei()
    obj.reverse_engineer()
